package com.wishwingz.homeworkchat;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.packet.Message;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.wishwingz.homeworkchat.XMPPManager.OnMessageReceiveListener;

public class MainActivity extends SherlockFragmentActivity {

	static final String TAG = "Debug";
	static final String TAB_FRIENDS_TAG = "friends";
	static final String TAB_CHAT_TAG = "chat";
	static final String TAB_FIND_TAG = "find";
	static final String TAB_MORE_TAG = "more";
	static final String SHARED_PREF_LOGINDATA = "loginData";
	static final String SHARED_PREF_LOGINYN = "loginYN";
	private SharedPreferences sharePref;
	private boolean loginYN;
	private TabHost mTabHost;
	private TabManager mTabManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		sharePref = getSharedPreferences(SHARED_PREF_LOGINDATA,
				MainActivity.MODE_PRIVATE);
		loginYN = sharePref.getBoolean(SHARED_PREF_LOGINYN, false);
		Log.d(TAG, String.valueOf(loginYN));

		setContentView(R.layout.activity_main);
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();

		mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);

		mTabManager.addTab(
				mTabHost.newTabSpec(TAB_FRIENDS_TAG).setIndicator("Friends"),
				FriendTabFragment.class, null);
		mTabManager.addTab(
				mTabHost.newTabSpec(TAB_CHAT_TAG).setIndicator("Chats"),
				DefaultFragment.class, null);
		mTabManager.addTab(
				mTabHost.newTabSpec(TAB_FIND_TAG).setIndicator("Find"),
				DefaultFragment.class, null);
		mTabManager.addTab(
				mTabHost.newTabSpec(TAB_MORE_TAG).setIndicator("More"),
				MoreTabFragment.class, null);

		if (loginYN) {
			mTabManager.setDefaultTab(TAB_FRIENDS_TAG);
		} else {
			mTabManager.setDefaultTab(TAB_MORE_TAG);
		}

		if (savedInstanceState != null) {
			if (loginYN) {
				mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
			} else {
				mTabManager.setDefaultTab(TAB_MORE_TAG);
			}
		}

		XMPPManager.getInstance().addOnMessageReceiveListener(
				new OnMessageReceiveListener() {

					@Override
					public void onMessageReceived(Chat chat, Message message) {
						// TODO Auto-generated method stub
						Toast.makeText(MainActivity.this,
								"message : " + message.getBody(),
								Toast.LENGTH_SHORT).show();
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getSupportMenuInflater().inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}
}
